<template>
  <el-dialog
    :title="'查看'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="项目名称" prop="projectName">
      <el-tag size="medium">
        {{dataForm.projectName}}
      </el-tag>
    </el-form-item>
    <el-form-item label="项目简介" prop="projectDesc">
      <el-tag size="medium">
        {{dataForm.projectDesc}}
      </el-tag>
    </el-form-item>
    <el-form-item label="项目亮点" prop="projectHighlight">
      <el-tag size="medium">
        {{dataForm.projectHighlight}}
      </el-tag>
    </el-form-item>
    <el-form-item label="项目时间" prop="projectTime">
      <el-tag size="medium">
        {{dataForm.projectTime}}
      </el-tag>
    </el-form-item>
    <el-form-item label="申请奖项" prop="prizeId">
      <el-select v-model="dataForm.prizeId" filterable placeholder="请选择" disabled>
        <el-option
          v-for="item in prizes"
          :key="item.id"
          :label="item.prizeName"
          :value="item.id">
        </el-option>
		  </el-select>
    </el-form-item>
    <el-form-item label="附件" prop="fileList">
      <div v-for="downLoadFile in fileList" :key="downLoadFile.id">
        <a @click="downFile(downLoadFile.fileStorageName)" download="w3logo" href="javascript:void(0);">
          {{downLoadFile.fileOriginName}}
        </a>
        <el-button type="danger" v-if="isAuth('generator:projectfile:deleteSingle') && sessionUserId != 1 && dataForm.state == 0" size="small" @click="deleteFileHandle(downLoadFile.id)">删除</el-button>
      </div>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">返回</el-button>
      <!--<el-button type="primary" @click="dataFormSubmit()">确定</el-button>-->
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        sessionUserId: 0,
        visible: false,
        prizes: [],
        fileList: [],
        dataForm: {
          id: 0,
          projectName: '',
          projectDesc: '',
          projectHighlight: '',
          projectTime: '',
          companyId: '',
          userId: '',
          prizeId: '',
          createTime: '',
          state: '',
          file: '',
          experts: [],
          mark: ''
        },
        dataRule: {
          prizeId: [
            { required: true, message: '请选择一个申请的奖项', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id, prizes) {
        this.prizes = prizes
        this.dataForm.id = id || 0
        this.visible = true
        this.sessionUserId = this.$store.state.user.id
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/generator/project/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.fileList = data.fileList
                this.dataForm.projectName = data.project.projectName
                this.dataForm.projectDesc = data.project.projectDesc
                this.dataForm.projectHighlight = data.project.projectHighlight
                this.dataForm.projectTime = data.project.projectTime
                this.dataForm.prizeId = data.project.prizeId
                this.dataForm.createTime = data.project.createTime
                this.dataForm.state = data.project.state
                this.dataForm.file = data.project.file
                this.dataForm.experts = data.project.experts
                this.dataForm.mark = data.project.mark
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            console.log('this.dataForm.experts == ')
            console.log(this.dataForm.experts)
            let expertStr = ''
            for (let i = 0; i < this.dataForm.experts.length; i++) {
              if ( i == this.dataForm.experts.length - 1) {
                expertStr += this.dataForm.experts[i]
              } else {
                expertStr += this.dataForm.experts[i] + ','
              }
              
            }
            this.$http({
              url: this.$http.adornUrl(`/generator/project/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'projectName': this.dataForm.projectName,
                'projectDesc': this.dataForm.projectDesc,
                'projectHighlight': this.dataForm.projectHighlight,
                'projectTime': this.dataForm.projectTime,
                'prizeId': this.dataForm.prizeId,
                'file': this.dataForm.file,
                'experts': expertStr
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      // 下载附件
      downFile(fileName){
        let a = document.createElement('a')
        a.href = this.$http.adornUrl(`/static/upload/` + fileName)
        a.click()
        return false;
      },
      // 删除附件
      deleteFileHandle (id) {
        this.$http({
            url: this.$http.adornUrl(`/generator/projectfile/deleteSingle/${id}`),
            method: 'get'
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.init(this.dataForm.id, this.prizes)
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
      }
    }
  }
</script>
